package hz.mobileproject.pda.ui.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.tree.FileBean;
import hz.mobileproject.pda.tree.Node;
import hz.mobileproject.pda.tree.SimpleTreeAdapter;
import hz.mobileproject.pda.tree.TreeListViewAdapter;

public class DeptSelectorDialog extends BaseCustomDialog implements TreeListViewAdapter.OnTreeNodeClickListener {
    @ViewInject(R.id.lv_dept_selector)
    private ListView lv_dept_selector;
    @ViewInject(R.id.tv_select_dept_name)
    private TextView tv_select_dept_name;

    private List<FileBean> mFileBeanList;
    private SimpleTreeAdapter<FileBean> mAdapter;
    private Node mSelectedNode;
    private OnPositiveBtnClick onPositiveBtnClick;

    public DeptSelectorDialog(Context context, List<FileBean> list) {
        super(context);
        this.mFileBeanList = list;
    }

    @Override
    protected int bindView() {
        return R.layout.dialog_dept_selector;
    }

    @Override
    protected void initData() {
        try {
            mAdapter = new SimpleTreeAdapter<>(lv_dept_selector, mContext, mFileBeanList, 1);
            lv_dept_selector.setAdapter(mAdapter);
            mAdapter.setOnTreeNodeClickListener(this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Event({R.id.btn_negative, R.id.btn_positive})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.btn_negative:
                this.dismiss();
                break;
            case R.id.btn_positive:
                if(mSelectedNode == null){
                    ToastUtils.showShort("未选择部门");
                    return;
                }
                if (onPositiveBtnClick != null) {
                    onPositiveBtnClick.onClick(this, mSelectedNode);
                }
                break;
        }
    }

    //listview点击事件
    @Override
    public void onClick(Node node, int position) {
        mSelectedNode = node;
        StringBuffer deptName = new StringBuffer(node.getName());
        tv_select_dept_name.setText(deptName.toString());
    }

    public interface OnPositiveBtnClick {
        void onClick(DeptSelectorDialog dialog, Node node);
    }

    public OnPositiveBtnClick getOnPositiveBtnClick() {
        return onPositiveBtnClick;
    }

    public void setOnPositiveBtnClick(OnPositiveBtnClick onPositiveBtnClick) {
        this.onPositiveBtnClick = onPositiveBtnClick;
    }
}
